(* Try to implement using a closure and references to mutate state. *)

class fibonacci =
    object (self)
        val mutable prev1 = 1;
        val mutable prev2 = 0;

        method next =
            let result = prev2 in
            prev2 <- prev1;
            prev1 <- prev2 + result;
            result
    end
;;


let fib = new fibonacci in
for x = 1 to (int_of_string Sys.argv.(1) + 1) do
    print_string (string_of_int fib#next ^ " ")
done;
print_endline "...";;
